package com.zmn.brs.business.interfaces.record;

import com.alibaba.fastjson.JSONObject;
import com.zmn.brs.common.constant.BrsConst;
import com.zmn.brs.common.enums.ThresholdExpressionEnum;
import com.zmn.brs.drools.bean.Frequency;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 类描述：阀值基础业务接口类
 *
 * @author xujie
 * @since 2021/03/23 10:28
 */
public interface ThresholdBService {

    /**
     * 阀值名称转换
     *
     * @param thresholdName 阀值名称
     * @param expression 阀值表达式
     * @param thresholdValue 阀值：值
     * @return 具体操作名称
     */
    static String thresholdValueTranslate(String thresholdName, String expression, String thresholdValue) {

        ThresholdExpressionEnum expressionEnum = ThresholdExpressionEnum.getExpressionEnum(expression);
        switch (expressionEnum) {
            case NOT_IN:
            case ALL:
            case NOT_EQUALS:
                return thresholdName;
            case TIME_FRAME:
                String [] times = thresholdValue.split(BrsConst.TILDE);
                if (times.length < 2) {
                    return thresholdValue;
                }
                String st = times[0];
                String et = times[1];
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                try {
                    Date startTime = sdf.parse(st);
                    Date endTime = sdf.parse(et);
                    // 结束时间小于等于开始时间，跨天
                    if (endTime.compareTo(startTime) <= 0) {
                        return st + BrsConst.TILDE + "次日" + et;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return thresholdValue;
            case GREATER_THAN_EQUAL:
                Frequency frequency = JSONObject.parseObject(thresholdValue, Frequency.class);
                if (frequency == null || frequency.getTime() == null || frequency.getFrequencyConfig() == null) {
                    return "";
                }
                return frequency.getTime() + "分钟" + frequency.getFrequencyConfig() + "次";
            default:
                return "";
        }
    }
}
